<?php
namespace app\extensions\lib\push;
use app\exception\ApiException;
use app\extensions\lib\Fcm;
use app\extensions\lib\FcmAccessToken;
use app\extensions\lib\Message;
use app\model\Package;
use app\model\TokenInfo;
use support\Db;
use support\Log;

class AndroidPush extends BasePush
{
    protected $config = [];

    private static function initConfig(){
        //Package::getPackageConfig(1);;
    }

    public static function push($tokenInfo, array $message,$request_id='')
    {
        self::blackSkip($tokenInfo,$request_id);
        self::requestRecall($tokenInfo,$request_id);
        if(!$tokenInfo['tokens']) return self::defaultReturn();
        /*if(true){
            $time = 1000;
            $count = count(explode(',',$tokenInfo['tokens']));
            $total_time = $time*$count;
            $response = 'test success';
            Log::channel('push')->info($request_id.':test_fcm:'.$count."===time:$total_time:".json_encode($response));
            Log::channel('fcm')->info($request_id.':test_fcm:'.$count."===time:$total_time:".json_encode($response));
            usleep($total_time);
            return self::defaultReturn($count);
        }*/
        $authToken = self::getPackageIdentification($tokenInfo['package_id'],$tokenInfo['is_async']);
        return self::fcmPush($tokenInfo['package_id'],$authToken,$tokenInfo,$message,$request_id);
    }

    private static function getPackageIdentification($package_id,$is_async)
    {
        $package_configList = Package::getPackageConfig(1);

        //fcm 没配置
        $configuration = $package_configList[$package_id]['configuration'] ?? '';
        $huawei = $package_configList[$package_id][Package::HUAWEI_CHANNEL_NAME] ?? '';
        if(!$configuration && !$huawei){
            throw new ApiException("包：{$package_id}无对应配置1");
        }
        if($configuration){
            $configuration = json_decode($configuration,true) ?: [];
            //return 'FcmAccessToken';
            return FcmAccessToken::getAccessToken($package_id,$is_async,$configuration);
        }else{
            return HmsPush::getPackageIdentification($package_id,$is_async);//使用huawei
        }


    }

    private static function fcmPush($package_id,$authToken,$tokenInfo,$message,$request_id=''){

        $package_configList = Package::getPackageConfig(1);
        $configuration = $package_configList[$package_id]['configuration'] ?? '';
        $huawei = $package_configList[$package_id][Package::HUAWEI_CHANNEL_NAME] ?? '';
        if(!$configuration && !$huawei){
            throw new ApiException("包：{$package_id}无对应配置");
        }

        if($configuration){
            $configuration = json_decode($configuration,true) ?: [];
            if(!isset($configuration['project_id'])){
                throw new ApiException("包：{$package_id}无对应工程project_id");
            }
            $result = Fcm::push($configuration['project_id'],$authToken,$message,$package_id,$request_id);
            return $result;
        }else{
            return HmsPush::send($package_id,$authToken,$tokenInfo,$message);
        }


    }

}